package com.chaos.eurekaproducer.leetCode;

import com.chaos.eurekaproducer.basic.ListNode;

/**
 * @author liaopeng
 * @title: Offer25
 * @projectName eureka-producer
 * @description:
 * 输入两个递增排序的链表，合并这两个链表并使新链表中的节点仍然是递增排序的。
 *
 * 示例1：
 * 输入：1->2->4, 1->3->4
 * 输出：1->1->2->3->4->4
 *
 * @date 2021/6/11上午9:47
 */
public class Offer25 {
    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode node = new ListNode(0);
        ListNode cur=node;
        while (l1!=null && l2!=null){
            if (l1.getVal()<l2.getVal()){
                cur.setNext(l1);
                l1 = l1.getNext();
            }else{
                cur.setNext(l2);
                l2 = l2.getNext();
            }
            cur = cur.getNext();
        }
        ListNode temp = l1==null?l2:l1;
        cur.setNext(temp);
        return node.getNext();
    }

    public static void main(String[] args) {
        ListNode l11 = new ListNode(1);
        ListNode l12 = new ListNode(2);
        ListNode l14= new ListNode(4);
        ListNode l21 = new ListNode(1);
        ListNode l23 = new ListNode(3);
        ListNode l24 = new ListNode(4);
        l12.setNext(l14);
        l11.setNext(l12);
        l23.setNext(l24);
        l21.setNext(l23);
        ListNode listNode = mergeTwoLists(l11, l21);
        System.out.println(listNode);
    }
}
